#include "../common.h"
using namespace std;
extern BuiltinCmd blt[MAX_BUILTIN_FUNC_NUM];
extern obj_index _index;
extern obj_index indexs[MAX_REFS_HEADS_NUMS];
extern char buf[80];
extern char _obj[MAX_BUILTIN_DESC_LENGTH];
extern char _GIT[MAX_BUILTIN_DESC_LENGTH];
extern char txt_index[MAX_BUILTIN_DESC_LENGTH];
extern char txt_HEAD[MAX_BUILTIN_DESC_LENGTH];
extern char _refs[MAX_BUILTIN_DESC_LENGTH];
extern char refs_heads[MAX_BUILTIN_DESC_LENGTH];
extern char _log[MAX_BUILTIN_DESC_LENGTH];
extern char log_logs[MAX_BUILTIN_DESC_LENGTH];
extern REFS_heads _heads[MAX_REFS_HEADS_NUMS];
extern char SHA_1[41];
extern char obj_sha[1024];
extern int indexnum;
char ADDR[MAX_BUILTIN_DESC_LENGTH];
bool FLAG=false;
obj_index INDEX_;

int git_status() {
    BuiltinCmd x;
    strcpy(x.name, "status");
    strcpy(x.desc, "find the file");
    x.hasAction = false;
    x.optNum = 2;
    strcpy(x.Help,"git status");
	strcpy(x.action, "git status");
	addBuiltinCmd(&x);
}

int getsha(char *retv){
	getcwd(buf, sizeof(buf));
	char ch[1024];strcpy(ch,buf);
	strcat(ch,"\\"); strcat(ch,retv);
	strcpy(retv,ch);
//	printf("%s\n",retv[3]);
	FILE *fp=fopen("D:\\self-git\\git\\sha1\\sha.txt","w");
	fwrite(retv,1,strlen(retv),fp);
	fclose(fp);
	system("D:\\self-git\\git\\sha1\\sha1.exe");
	fp=fopen("D:\\self-git\\git\\sha1\\sha_write.txt","r");
	  fgets(SHA_1,41,fp);
	fclose(fp); 
}

int git_status_cmdpline(){return 0;}
int git_status_run(){
	_init();
	 strcpy(ADDR,buf);strcat(ADDR,"\\*.txt");char* SreachAddr=ADDR;
	 long Handle;
	struct _finddata_t FileInfo;
	Handle=_findfirst(SreachAddr,&FileInfo);
	if(-1==Handle){
		printf("On branch master\n");
		printf("nothing to commit, working tree clean\n");
		return -1;
	}
	getsha(FileInfo.name);
	bool flag=false;
	FILE *fp=fopen(txt_index,"r");
	while(fread(&_index,sizeof(_index),1,fp)){
		if (strcmp(_index.tree,SHA_1)==0){flag=true;break;}
	}
	fclose(fp);
	if (flag==false){
		FLAG=true;
		printf("On branch master\n");
		printf("Changes not staged for commit:\n");
		printf("  (use \"git add <file>...\" to update what will be committed)\n");
		printf("  (use \"git restore <file>...\" to discard changes in working directory)\n");
		printf("        modified:   %s\n\n",FileInfo.name);
		printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
	}

	char refs_path[MAX_BUILTIN_DESC_LENGTH]="";
	fp=fopen(txt_HEAD,"r");  
	fgets(refs_path,MAX_BUILTIN_DESC_LENGTH,fp);
	fclose(fp);
	fp=fopen(refs_path,"r");
	fgets(refs_path,MAX_BUILTIN_DESC_LENGTH,fp); 
	fclose(fp);
	fp=fopen(txt_index,"r");
	while(fread(&_index,sizeof(_index),1,fp)){
	    flag=false;
		FILE *fp1=fopen(refs_path,"r");fread(&INDEX_,sizeof(INDEX_),1,fp1);
		while(fread(&INDEX_,sizeof(INDEX_),1,fp1)){
			if (strcmp(_index.tree,INDEX_.tree)==0){flag=true;break;}
		}
		fclose(fp1);
		if (flag==false){
		  FLAG=true;
		  printf("On branch master\n");
		  printf("Changes to be committed:\n");
		  printf("  (use \"git restore <file>...\" to discard changes in working directory)\n");
		  printf("        modified:   %s\n\n",_index.file_path);
	    }
	}
	fclose(fp);
	
	FILE *fp1=fopen(refs_path,"r");fread(&INDEX_,sizeof(INDEX_),1,fp1);
	while(fread(&INDEX_,sizeof(INDEX_),1,fp1)){
	    flag=false;
		fp=fopen(txt_index,"r");
		while(fread(&_index,sizeof(_index),1,fp)){
			if (strcmp(_index.file_path,INDEX_.file_path)==0){flag=true;break;}
		}
		fclose(fp);
		if (flag==false){
		  FLAG=true;
		  printf("On branch master\n");
		  printf("Changes to be committed:\n");
		  printf("  (use \"git restore <file>...\" to discard changes in working directory)\n");
		  printf("        deleted:   %s\n\n",INDEX_.file_path);
	    }
	}
	fclose(fp1);

	
	while(!_findnext(Handle,&FileInfo))
	{
		getsha(FileInfo.name);
	//printf("%s\n",SHA_1);
	//	printf("%s\n",FileInfo.name);
		bool flag=false;
	    FILE *fp=fopen(txt_index,"r");
	    while(fread(&_index,sizeof(_index),1,fp)){
		  if (strcmp(_index.tree,SHA_1)==0){flag=true;break;}
	    }
	   fclose(fp);
	    if (flag==false){
	      FLAG=true;
		  printf("On branch master\n");
		  printf("Changes not staged for commit:\n");
		  printf("  (use \"git add <file>...\" to update what will be committed)\n");
		  printf("  (use \"git restore <file>...\" to discard changes in working directory)\n");
		  printf("        modified:   %s\n\n",FileInfo.name);
		  printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
	    }
	}
	_findclose(Handle);
	if (FLAG==false){
		printf("On branch master\n");
		printf("nothing to commit, working tree clean\n");
	}
    
}
